package com.quyunshuo.module.home.fragment.me

import android.app.ProgressDialog.show
import android.content.Intent
import android.text.TextUtils
import android.util.Log
import androidx.core.content.ContextCompat
import androidx.fragment.app.viewModels
import com.quyunshuo.androidbaseframemvvm.base.BaseApplication
import com.quyunshuo.androidbaseframemvvm.base.utils.SpUtils
import com.quyunshuo.androidbaseframemvvm.common.constant.USER_INFO_DATA
import com.quyunshuo.androidbaseframemvvm.common.ext.click
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseFragment
import com.quyunshuo.androidbaseframemvvm.network.bean.UserInfo
import com.quyunshuo.module.home.R
import com.quyunshuo.module.home.activity.login.LoginActivity
import com.quyunshuo.module.home.databinding.HomeFragmentMeBinding
import com.quyunshuo.module.home.dialog.MessageDialog
import com.quyunshuo.module.home.dialog.MyDialogFragment
import com.quyunshuo.module.home.provider.UserServiceProvider
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author DBoy 2021/7/6 <p>
 * - 文件描述 : 测试fragment
 */
@AndroidEntryPoint
class MeFragment : BaseFragment<HomeFragmentMeBinding, MeViewModel>() {
    private val TAG = "InternalFragment"
    override val mViewModel by viewModels<MeViewModel>()
    private var userInfo: UserInfo? = null;
    override fun createVB() = HomeFragmentMeBinding.inflate(layoutInflater)

    override fun HomeFragmentMeBinding.initView() {
        userInfo = SpUtils.getParcelable<UserInfo>(USER_INFO_DATA, UserInfo())
        tvUsername.text = userInfo?.username
        tvFollowing.text = userInfo?.coinCount + "关注"
        tvFollowers.text = userInfo?.coinCount + "粉丝"
        clAboutUs.click {
            //跳转到个人页，如果还没登录，则跳转到登录页
            if (TextUtils.isEmpty(userInfo?.username)) {
                //跳转到登录页
                startActivity(Intent(activity, LoginActivity::class.java))
            } else {
                //跳转到个人页。
                showClearCacheDialog()
//                ToastUtil.showToast("开发中")
            }
            //如何判断呢？cookie，以及个人信息？？？？对的。两个都要。只需要个人信息。
        }
        UserServiceProvider.getUserLiveData().observe(this@MeFragment) { userInfo ->
            this@MeFragment.userInfo = userInfo
            tvUsername.text = userInfo.username
            tvFollowing.text = userInfo.coinCount + "关注"
            tvFollowers.text = userInfo.coinCount + "粉丝"
        }

    }

    /**
     * 清理缓存弹框
     */
    private fun showClearCacheDialog() {
        MyDialogFragment().show(this.parentFragmentManager, this.javaClass.name)

//        MessageDialog.Builder(requireActivity()).setTitle("温馨提示")
//            .setMessage("确定？")
//            .setConfirm("确定")
//            .setConfirmTxtColor(ContextCompat.getColor(BaseApplication.application, R.color.accentColor))
//            .setCancel("取消")
//            .setonCancelListener {
//                it?.dismiss()
//            }
//            .setonConfirmListener {
//                it?.dismiss()
//            }.create().show()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        Log.d(TAG, "onDestroyView: ")
    }

    override fun initObserve() {
    }

    override fun initRequestData() {
    }
}